package com.haixiaoke.saas.manage.tenant.controller;

import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.tenant.domain.Tenant;
import com.haixiaoke.saas.tenant.domain.TenantTree;
import com.haixiaoke.saas.tenant.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 机构信息Controller
 *
 * @author panda
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/manage/tenant/tenant")
public class ManageTenantController extends BaseController {
    @Autowired
    private ITenantService tenantService;

    /**
     * 获取机构信息树状列表
     * @param tenant
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manage:dept:list')")
    @GetMapping("/tree/list")
    @TenantScope
    public AjaxResult treeList(Tenant tenant) {
        List<TenantTree> list = tenantService.buildTreeList(tenant);
        return success(list);
    }

    /**
     * 获取机构信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:tenant:query')")
    @GetMapping("/{tenantId}")
    public AjaxResult getInfo(@PathVariable("tenantId") String tenantId) {
        return success(tenantService.selectDepByDepId(tenantId));
    }

    /**
     * 新增机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:tenant:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Tenant department) {
        return tenantService.insertDepartment(department);
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:tenant:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Tenant department) {
        return tenantService.manageUpdateDepartment(department);
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:tenant:edit')")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Tenant tenant) {
        tenant.setPassword(null);
        return toAjax(tenantService.updateTenant(tenant));
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:tenant:edit')")
    @PutMapping("/editState/{tenantId}")
    public AjaxResult editState(@PathVariable("tenantId") String tenantId) {
        return tenantService.editTenantState(tenantId, getUsername());
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('manage:tenant:edit')")
    @PutMapping("/editBillState/{tenantId}")
    public AjaxResult editBillState(@PathVariable("tenantId") String tenantId) {
        return tenantService.editBillState(tenantId, getUsername());
    }
}
